package com.itheima.mybatis.framework;

import org.apache.ibatis.annotations.Select;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;

/**
 * @author ：sean
 * @date ：Created in 2020/6/29
 * @description ：SqlSession默认实现
 * @version: 1.0
 */
public class DefaultHmSqlSession implements HmSqlSession {

  private HmConfiguration configuration;
  private HmExecutor executor;



  public DefaultHmSqlSession(HmConfiguration configuration) {
    this.configuration = configuration;
    this.executor = new HmExecutor(configuration);
  }

  @Override
  public <T> List<T> selectList(String mapperId)
  {
    try {
      List<T> objects = executor.executeQuery(mapperId);
      return objects;
    }catch (Exception e){
      e.printStackTrace();
    }
    return null;
  }


  @Override
  public <T> T getMapper(Class<T> type) {
    Object  obj = configuration.getMapperRegistry().getMapper(type,this);
    //Class clazz[] = new Class[]{type};
    //Object  obj = Proxy.newProxyInstance(this.getClass().getClassLoader(), clazz,
    //  // 动态实现
    //  new InvocationHandler() {
    //    @Override
    //    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    //      // 动态实现方法内容
    //      String pre = type.getName()+"."+method.getName();
    //      System.out.println(pre);
    //      List<T> objects = executor.executeQuery(pre);
    //      // 动态方法的返回值
    //      return objects;
    //    }
    //  });
    return (T)obj;
  }

  @Override
  public void close() {

  }

  @Override
  public void commit() {

  }
}
